package com.weeho.dongyong.service;

import com.weeho.dongyong.dao.OrganizationDao;
import com.weeho.dongyong.entity.Organization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrganizationService {
    @Autowired
    private OrganizationDao organizationDao;

    public int insertSelective(Organization record) {
        return organizationDao.insertSelective(record);
    }

    public int updateByPrimaryKeySelective(Organization record) {
        return organizationDao.updateByPrimaryKeySelective(record);
    }

    public int deleteByPrimaryKey(long id) {
        return organizationDao.deleteByPrimaryKey(id);
    }

    public Organization selectByPrimaryKey(long id) {
        return organizationDao.selectByPrimaryKey(id);
    }

    public List<Organization> selectByCondition(Organization record) {
        return organizationDao.selectByCondition(record);
    }

    public List<Organization> getAll(){
        return selectByCondition(null);
    }


    public Organization getOne(String userId){
        Organization organization = new Organization();
        organization.setUserId(userId);
        List<Organization> list = selectByCondition(organization);
        if(list != null && list.size()>0){
            return list.get(0);

        }
        return null;
    }

    public Organization getNextLevel(int level){
        Organization organization = new Organization();
        organization.setOrganLevel(new BigDecimal(level+1));

        List<Organization> list = selectByCondition(organization);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    public Organization getNextLevel(Organization organization){
      return getNextLevel(organization.getOrganLevel());
    }
    public Organization getNextLevel(BigDecimal bigDecimal){
        return getNextLevel(bigDecimal.intValue());
    }

//    public Organization getNextLevel(String approId){
//
//    }
    public Organization getMaxLevel(){

        Organization organization = null;
        List<Organization> list = getAll();
        if(list != null && list.size()>0){
            organization = list.get(0);
            for(int i=1;i<list.size();i++){
                if(organization.getOrganLevel().compareTo(list.get(i).getOrganLevel()) == -1){
                    organization = list.get(i);
                }
            }
        }
        return organization;
    }

    public Organization getFirstLevel(){
        Organization organization = new Organization();
        organization.setOrganLevel(new BigDecimal(1));

        List<Organization> list = selectByCondition(organization);
        if(list != null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
}